﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using AfterlifeLib;
using AfterlifeLib.Lighting;
using AfterlifeLib.Collision;
using AfterlifeLib.Weapons;
using AfterlifeLib.Input;

namespace Afterlife.Entities
{
    public class Player : Entity
    {
        private int health = 100;
        public int Health
        {
            get { return health; }
            set
            {
                if (value >= 0)
                    health = value;
            }
        }

        private Camera2D m_camera;
        public Camera2D Camera
        {
            get { return m_camera; }
        }

        private Gun weapon;
        public Gun Weapon
        {
            get { return weapon; }
            set
            {
                if (value != null)
                {
                    value.Owner = this;
                    weapon = value;
                }
            }
        }

        private Light flashLight = new Light("FlashLight", Color.White, 300, 1, Vector3.Zero);
        public Light FlashLight
        {
            get { return flashLight; }
        }

        public Player(World world) : base(world)
        {
            m_camera = new Camera2D(Vector2.Zero);
        }

        public void Initialize(Vector2 position, float speed)
        {
            Position = position;
            Speed = speed;

            m_camera.CenterPosition(Position);
        }

        public override void LoadContent(ContentManager content)
        {
            Texture = content.Load<Texture2D>("Images/Sprites/player");
            Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
            Radius = Texture.Width / 2; //Hallo ik ben ruben

            Weapon = (Gun)GetWorld<AfterlifeWorld>().LoadedWeapons["Colt_M4_Carbine"];
            Weapon.Initialize(weapon.AmmoPerClip, 500);
        }

        public override void Update(GameTime gameTime)
        {
            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Velocity = (GetMotionVector() * Speed) * delta;

            Vector2 newPosition = CollisionHelper.GetNewPosition(
                GetWorld<AfterlifeWorld>().CurrentLevel.CollisionLayer, 
                Position, 
                Velocity, 
                Texture.Width, 
                Texture.Height);

            Position = newPosition;
            m_camera.CenterPosition(newPosition);
            m_camera.ClampPosition(GetWorld<AfterlifeWorld>().CurrentLevel);
            flashLight.Position = new Vector3(newPosition + Origin, 0);

            if (InputState.IsKeyFirstDown(Keys.R))
                weapon.Reload();

            weapon.Update(gameTime);

            HandleMouse();
        }

        public override void Draw(SpriteBatch spriteBatch, Camera2D camera)
        {
            spriteBatch.Begin();
            {
                spriteBatch.Draw(
                    Texture,
                    Position - m_camera.PositionV2 + Origin,
                    null,
                    Color.White,
                    Rotation + MathHelper.PiOver2,
                    Origin,
                    1,
                    SpriteEffects.None,
                    0);
            }
            spriteBatch.End();
        }

        private Vector2 GetMotionVector()
        {
            Vector2 motion = Vector2.Zero;
            if (InputState.IsKeyDown(Keys.A))
                motion.X -= 1;
            if (InputState.IsKeyDown(Keys.D))
                motion.X += 1;
            if (InputState.IsKeyDown(Keys.W))
                motion.Y -= 1;
            if (InputState.IsKeyDown(Keys.S))
                motion.Y += 1;

            if (motion != Vector2.Zero)
                motion.Normalize();

            return motion;
        }

        private void HandleMouse()
        {
            float mdx = InputState.MouseX - (Position.X + Origin.X - Camera.PositionV2.X);
            float mdy = InputState.MouseY - (Position.Y + Origin.Y - Camera.PositionV2.Y);

            Rotation = (float)Math.Atan2(mdy, mdx);

            if (InputState.IsButtonPressed(MouseButtons.Left))
            {
                Weapon.Fire(World.Get<AfterlifeWorld>(World).ProjectileManager, Position + Origin, Rotation);
            }
        }
    }
}
